package jasepro;

public class HttpReplyHdr {

	static String CR = "\r\n";
	static String HTTP_PROTOCOL = "HTTP/1.1";
	static String HTTP_SERVER = "Java Proxy Server";

	String lastModified = "";
	long contentLength = 0;
	String extraErrorString = "";

	public boolean setModifiedDate(String date) {
		lastModified = date;
		return true;
	}

	public boolean addErrorDescription(String str) {
		extraErrorString = str;
		return true;
	}

	public String formOk(String ContentType, long ContentLength) {

		contentLength = ContentLength;

		String out = new String();

		out += HTTP_PROTOCOL + " 200 Ok" + CR;
		out += "Server: " + HTTP_SERVER + CR;
		out += "MIME-version: 1.0" + CR;

		if (0 < ContentType.length())
			out += "Content-type: " + ContentType + CR;
		else
			out += "Content-Type: text/html" + CR;

		if (0 != contentLength)
			out += "Content-Length: " + Long.toString(contentLength) + CR;

		if (0 < lastModified.length())
			out += "Last-Modified: " + lastModified + CR;

		out += CR;

		return out;
	}

	private String formErrorBody(String Error, String Description) {
		String out;
		// Generate Error Body
		out = "<HTML><HEAD><TITLE>";
		out += Error;
		out += "</TITLE></HEAD>";
		out += "<BODY><H2>" + Error + "</H2>\n";
		out += "</P></H3>";
		out += Description;
		out += "</BODY></HTML>";
		return out;
	}

	private String formError(String Error, String Description) {
		String body = formErrorBody(Error, Description);
		String header = new String();

		header += HTTP_PROTOCOL + " " + Error + CR;
		header += "Server: " + HTTP_SERVER + CR;
		header += "MIME-version: 1.0" + CR;
		header += "Content-type: text/html" + CR;

		if (0 < lastModified.length())
			header += "Last-Modified: " + lastModified + CR;

		header += "Content-Length: " + String.valueOf(body.length()) + CR;

		header += CR;
		header += body;

		return header;
	}

	public String formCreated() {
		return formError("201 Created", "Object was created");
	}

	public String formAccepted() {
		return formError("202 Accepted", "Object checked in");
	}

	public String formPartial() {
		return formError("203 Partial", "Only partail document available");
	}

	public String formMoved() {
		// 300 codes tell client to do actions
		return formError("301 Moved", "File has moved");
	}

	public String formFound() {
		return formError("302 Found", "Object was found");
	}

	public String formMethod() {
		return formError("303 Method unseported", "Method unseported");
	}

	public String formNotModified() {
		return formError("304 Not modified", "Use local copy");
	}

	public String formUnautorized() {
		return formError("401 Unathorized", "Unathorized use of this service");
	}

	public String formPaymentNeeded() {
		return formError("402 Payment required", "Payment is required");
	}

	public String formForbidden() {
		return formError("403 Forbidden",
				"You need permission for this service");
	}

	public String formNotFound() {
		return formError("404 Not_found", "Requested object was not found");
	}

	public String formInternalError() {
		return formError("500 Internal server error", "Server broke");
	}

	public String formNotImplemented() {
		return formError("501 Method not implemented",
				"Service not implemented, programer was lazy");
	}

	public String formOverloaded() {
		return formError("502 Server overloaded", "Try again latter");
	}

	public String formTimeout() {
		return formError("503 Gateway timeout", "The connection timed out");
	}

	public String formServerNotFound() {
		return formError("503 Gateway timeout",
				"The requested server was not found");
	}

	public String formNotAllowed() {
		return formError("403 Access Denied", "Access is not allowed");
	}

	public String requestSSLAccepted() {
		String out = new String();

		out += HTTP_PROTOCOL + " 200 Connection established" + CR;
		out += "Server: " + HTTP_SERVER + CR;
		out += "MIME-version: 1.0" + CR;
		out += "Content-Type: text/html" + CR;
		out += CR;

		return out;
	}
}
